<?php

class Eleven_Resource_Student_Reply extends CMS_Model_Resource_Db_Table_Abstract
{
	protected $_name = 'studentreply';
	protected $_primary = 'id';
	protected $_rowClass = 'Eleven_Resource_Student_Reply_Item';
	
	protected $_referenceMap = array(
        'studentContent' => array(
            'columns'           => array('contentId'),
            'refTableClass'     => 'Eleven_Resource_Student_Content',
            'refColumns'        => array('id'),
            'onDelete'          => self::CASCADE,
        ),
    );
	
	public function getReplysByContentId($id, $paged = false, $order = null)
	{
	    $select = $this->select();
	    
	    $select->where('contentId = ?', $id);
	    $select->order('id ASC');
	    
	    if (null !== $paged) {
			$adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
			$count = clone $select;
			$count->reset ( Zend_Db_Select::COLUMNS );
			$count->reset ( Zend_Db_Select::FROM );
			$count->from ( 'studentreply', new Zend_Db_Expr ( 'COUNT(*) AS `zend_paginator_row_count`' ) );
			$adapter->setRowCount ( $count );
			
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 10 )->setCurrentPageNumber ( ( int ) $paged );
			return $paginator;
		}
	    
	    return $this->fetchAll($select);
	}
}

?>